package com.dwjm.shop.Wwj.Goods;


import com.google.gson.Gson;
import com.dwjm.shop.utils.CommonResult;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/queryByCondition")
public class QueryByConditionHandler extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        用什么方式显示汉字
        resp.setContentType("text/html;charset=UTF-8");

        String keyword=req.getParameter("keywords");
        GoodsDao goodsDao=new GoodsDaoImpl();
        List<Goods> goodsByKeyword=goodsDao.findGoodsByword(keyword);
        Gson gson=new Gson();

        if (goodsByKeyword.size()>0){
            CommonResult<List<Goods>> success=CommonResult.success(goodsByKeyword);
            String s=gson.toJson(success);
            resp.getWriter().write(s);
        }else {
            CommonResult<List<Goods>> fail=CommonResult.fail(500,"没有对应的数据");
            resp.getWriter().write(fail.getCode());
        }

    }
}
